package com.quanyan.stadium.biz.impl;

import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.stadium.biz.BizPKEventDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbPKEvent;
import com.quanyan.stadium.entity.TbPKEventExample;
import com.quanyan.stadium.entity.TbUserPKResult;
import com.quanyan.stadium.entity.TbUserPKResultExample;
import com.quanyan.stadium.entity.vo.PKEventVo;
import com.quanyan.stadium.entity.vo.PKResultVo;
import com.quanyan.stadium.mapper.TbPKEventMapper;
import com.quanyan.stadium.mapper.TbUserPKResultMapper;
import com.quanyan.stadium.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BizPKEventDaoImpl implements BizPKEventDao {

    private static final Logger logger = LoggerFactory.getLogger(BizPKEventDaoImpl.class);

    @Autowired
    private TbPKEventMapper tbPKEventMapper;
    @Autowired
    private TbUserPKResultMapper tbUserPKResultMapper;

    @Override
    public Integer createPKEvent(PKEventVo PKEventVo) {
        logger.info("createPKEvent reqPKEvent:" + PKEventVo.toString());

        TbPKEvent tbPKEvent = repPKEventVoChangeTbPkEvent(PKEventVo);
        tbPKEventMapper.insert(tbPKEvent);
        return tbPKEvent.getId();
    }

    @Override
    public List<TbPKEvent> queryUserPKList(Integer uid, Integer status) {

        TbPKEventExample example = new TbPKEventExample();

        // SQL fromUser = uid and status = status
        TbPKEventExample.Criteria criteria = example.createCriteria();
        criteria.andFromUidEqualTo(uid);
        criteria.andStatusEqualTo(status);
        // 三个月内的数据
        criteria.andBeginTimeGreaterThan(new Date(System.currentTimeMillis()-(3*30*24*60*60*1000)));

        // SQL toUser = uid and status = status
        TbPKEventExample.Criteria criteria1 = example.createCriteria();
        criteria1.andToUidEqualTo(uid);
        criteria1.andStatusEqualTo(status);

        // SQL or
        example.or(criteria1);

        //查询数据
        List<TbPKEvent> tbPKEventList = tbPKEventMapper.selectByExample(example);

        logger.info("queryUserPKList tbPKEventList:"+ tbPKEventList);

        return tbPKEventList;
    }

    @Override
    public List<TbPKEvent> queryUserCreatePKList(Integer uid, Integer status, Integer type) {

        TbPKEventExample example = new TbPKEventExample();
        TbPKEventExample.Criteria criteria = example.createCriteria();

        criteria.andFromUidEqualTo(uid);
        criteria.andTypeEqualTo(type);
        criteria.andStatusLessThanOrEqualTo(status);

        example.setOrderByClause("update_time desc");

        return tbPKEventMapper.selectByExample(example);
    }

    @Override
    public List<TbPKEvent> queryCludCreatePKList(Integer cludId, Integer status, Integer type) {
        TbPKEventExample example = new TbPKEventExample();
        TbPKEventExample.Criteria criteria = example.createCriteria();

        criteria.andFromClubIdEqualTo(cludId);
        criteria.andTypeEqualTo(type);
        criteria.andStatusLessThanOrEqualTo(status);

        example.setOrderByClause("update_time desc");

        return tbPKEventMapper.selectByExample(example);
    }

    @Override
    public List<TbPKEvent> queryUserReceivePKList(Integer uid, Integer status, Integer type) {

        TbPKEventExample tbPKEventExample = new TbPKEventExample();
        TbPKEventExample.Criteria criteria = tbPKEventExample.createCriteria();
        criteria.andToUidEqualTo(uid);
        criteria.andTypeEqualTo(type);
        criteria.andStatusLessThanOrEqualTo(status);

        tbPKEventExample.setOrderByClause("update_time desc");
        //查询数据
        List<TbPKEvent> tbPKEvrntList = tbPKEventMapper.selectByExample(tbPKEventExample);

        logger.info("queryUserCreatePKList PKEventVoList size:"+ tbPKEvrntList.size() +" result:" + tbPKEvrntList);

        return tbPKEvrntList;
    }

    @Override
    public List<TbPKEvent> queryCludReceivePKList(Integer cludId, Integer status, Integer type) {
        TbPKEventExample tbPKEventExample = new TbPKEventExample();
        TbPKEventExample.Criteria criteria = tbPKEventExample.createCriteria();
        criteria.andToClubIdEqualTo(cludId);
        criteria.andTypeEqualTo(type);
        criteria.andStatusLessThanOrEqualTo(status);

        tbPKEventExample.setOrderByClause("update_time desc");
        //查询数据
        List<TbPKEvent> tbPKEvrntList = tbPKEventMapper.selectByExample(tbPKEventExample);

        logger.info("queryUserCreatePKList PKEventVoList size:"+ tbPKEvrntList.size() +" result:" + tbPKEvrntList);

        return tbPKEvrntList;
    }

//    @Override
//    public List<TbPKEvent> queryCludReceivePKList(List<Integer> cludIdList, Integer status, Integer type) {
//        TbPKEventExample tbPKEventExample = new TbPKEventExample();
//        TbPKEventExample.Criteria criteria = tbPKEventExample.createCriteria();
//        criteria.andToClubIdIn(cludIdList);
//        criteria.andTypeEqualTo(type);
//        criteria.andStatusLessThanOrEqualTo(status);
//
//        tbPKEventExample.setOrderByClause("update_time desc");
//        //查询数据
//        List<TbPKEvent> tbPKEvrntList = tbPKEventMapper.selectByExample(tbPKEventExample);
//
//        logger.info("queryUserCreatePKList PKEventVoList size:"+ tbPKEvrntList.size() +" result:" + tbPKEvrntList);
//
//        return tbPKEvrntList;
//    }

    @Override
    public TbPKEvent queryUserNewestCreatePK(Integer uid,Integer categoryId) {
        TbPKEventExample tbPKEventExample = new TbPKEventExample();
        TbPKEventExample.Criteria criteria = tbPKEventExample.createCriteria();
        criteria.andToUidEqualTo(uid);
        criteria.andCategoryIdEqualTo(categoryId);
        tbPKEventExample.setOrderByClause("id desc");
        //查询数据
        List<TbPKEvent> tbPKEvrntList = tbPKEventMapper.selectByExample(tbPKEventExample);

        logger.info("queryUserCreatePKList PKEventVoList result:" + tbPKEvrntList);

        if (tbPKEvrntList == null || tbPKEvrntList.size() == 0) {
            return null;
        }

        return tbPKEvrntList.get(0);
    }

    @Override
    public TbPKEvent getPKInfoById(Integer id) {
        return tbPKEventMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updatePKStatusById(Integer id, Integer status) {
        return updatePKStatusById(id, status, null);
    }

    @Override
    public Integer updatePKStatusById(Integer id, Integer status, Long beginTime) {
        if (ObjectUtil.isObjectEmpty(id, status)) {
            logger.info("updatePKStatusById param id or status is null.");
            return 0;
        }

        TbPKEvent tbPKEvent = new TbPKEvent();
        tbPKEvent.setId(id);
        tbPKEvent.setStatus(status);
        if (ObjectUtil.isNotObjectEmpty(beginTime)) {
            tbPKEvent.setBeginTime(new Date(beginTime));
        }
        //设置修改时间
        tbPKEvent.setUpdateTime(new Date());

        // 持久化到数据库
        int result = tbPKEventMapper.updateByPrimaryKeySelective(tbPKEvent);

        logger.info("updatePKStatusById result:"+ result+" update id:"+ id+" status:"+ status);

        return result;
    }

    @Override
    public void entryPKResult(PKResultVo pkResultVo) {

        int result = tbUserPKResultMapper.insert(pkResultVoChangeTbPKResult(pkResultVo));

        System.out.println("entryPKResult inset result:"+ result);
        logger.info("entryPKResult inset result:"+ result);
    }

    @Override
    public List<PKResultVo> queryPkResultByPkid(Integer pkid) {

        TbUserPKResultExample example = new TbUserPKResultExample();
        TbUserPKResultExample.Criteria criteria = example.createCriteria();
        criteria.andPkIdEqualTo(pkid);

        List<TbUserPKResult> tbUserPKResultList = tbUserPKResultMapper.selectByExample(example);
        List<PKResultVo> pkResultVoList = new ArrayList<PKResultVo>();

        for (TbUserPKResult tbUserPKResult :  tbUserPKResultList) {
            pkResultVoList.add(pkResultVoChangeTbPKResult(tbUserPKResult));
        }

        return pkResultVoList;
    }

    @Override
    public PKResultVo queryPkResultByUidAndPkid(Integer uid, Integer pkid) {
        TbUserPKResultExample example = new TbUserPKResultExample();
        TbUserPKResultExample.Criteria criteria = example.createCriteria();
        criteria.andPkIdEqualTo(pkid);
        criteria.andUidEqualTo(uid);

        List<TbUserPKResult> tbUserPKResultList = tbUserPKResultMapper.selectByExample(example);

        PKResultVo pkResultVo = null;
        for (TbUserPKResult tbUserPKResult :  tbUserPKResultList) {
            pkResultVo = pkResultVoChangeTbPKResult(tbUserPKResult);
        }

        return pkResultVo;
    }

    @Override
    public void updatePKEvent(PKEventVo pkEventVo) {
        TbPKEvent tbPKEvent = repPKEventVoChangeTbPkEvent(pkEventVo);
        tbPKEvent.setUpdateTime(new Date());
        tbPKEventMapper.updateByPrimaryKeySelective(tbPKEvent);
    }

    private PKResultVo pkResultVoChangeTbPKResult(TbUserPKResult tbUserPKResult) {
        PKResultVo pkResultVo = new PKResultVo();
        pkResultVo.setId(tbUserPKResult.getPkId());
        pkResultVo.setUid(tbUserPKResult.getUid());
        pkResultVo.setPkResult(tbUserPKResult.getResult());
        pkResultVo.setEntryId(tbUserPKResult.getEntryId());

        return pkResultVo;
    }

    private TbUserPKResult pkResultVoChangeTbPKResult(PKResultVo pkResultVo) {
        TbUserPKResult tbUserPKResult = new TbUserPKResult();
        tbUserPKResult.setPkId(pkResultVo.getId());
        tbUserPKResult.setUid(pkResultVo.getUid());
        tbUserPKResult.setResult(pkResultVo.getPkResult());
        if (ObjectUtil.isNotObjectEmpty(pkResultVo.getEntryId())) {
            tbUserPKResult.setEntryId(pkResultVo.getEntryId());
        }
        tbUserPKResult.setCreateTime(new Date());
        tbUserPKResult.setUpdateTime(new Date());

        return tbUserPKResult;
    }

    private TbPKEvent repPKEventVoChangeTbPkEvent(PKEventVo pkEventVo) {

        TbPKEvent record = new TbPKEvent();
        record.setId(pkEventVo.getId());

        if (pkEventVo.getFromUser() != null) {
            record.setFromUid(pkEventVo.getFromUser().getUid());
        }

        if (pkEventVo.getToUser() != null) {
            record.setToUid(pkEventVo.getToUser().getUid());
        }

        if (pkEventVo.getType().byteValue() == GameConstants.TEAM_TYPE_CLUB) {
            record.setFromClubId(pkEventVo.getFromClub().getClubId());
            record.setToClubId(pkEventVo.getToClub().getClubId());
        }
        record.setAddress(pkEventVo.getAddress());
        record.setGameSystem(pkEventVo.getGameSystem());
        record.setCategoryId(pkEventVo.getCategoryId());
        record.setPayType(pkEventVo.getPayType());
        record.setType(pkEventVo.getType());
        record.setStatus(pkEventVo.getStatus());
        if (ObjectUtil.isNotObjectEmpty(pkEventVo.getBeginTime1()) && !pkEventVo.getBeginTime1().equals(0L)) {
            record.setBeginTime1(new Date(pkEventVo.getBeginTime1()));
        }
        if (ObjectUtil.isNotObjectEmpty(pkEventVo.getBeginTime2()) && !pkEventVo.getBeginTime2().equals(0L)) {
            record.setBeginTime2(new Date(pkEventVo.getBeginTime2()));
        }
        if (ObjectUtil.isNotObjectEmpty(pkEventVo.getBeginTime3()) && !pkEventVo.getBeginTime3().equals(0L)) {
            record.setBeginTime3(new Date(pkEventVo.getBeginTime3()));
        }
        record.setMessage(StringUtils.stringNullChange(pkEventVo.getMessage()));
        record.setCreateTime(pkEventVo.getCreateTime());
        record.setUpdateTime(pkEventVo.getUpdateTime());

        logger.info("repPKEventVoChangeTbPkEvent record id:"+record.getId());
        return record;
    }
}
